using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Deferred_Rendering
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Lights : Microsoft.Xna.Framework.GameComponent
    {
        private List<Light> lights;

        private Color[] colours = new Color[10];

        private Random randomNumber;

        public Lights(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            lights = new List<Light>();

            colours[0] = Color.ForestGreen;
            colours[1] = Color.Blue;
            colours[2] = Color.Pink;
            colours[3] = Color.Yellow;
            colours[4] = Color.Orange;
            colours[5] = Color.Green;
            colours[6] = Color.Crimson;
            colours[7] = Color.CornflowerBlue;
            colours[8] = Color.Gold;
            colours[9] = Color.Honeydew;

            randomNumber = new Random();


            base.Initialize();
        }

        public void addLights(GameTime gameTime)
        {
            float angle = (float)gameTime.TotalGameTime.TotalSeconds;

            int n = 2;
            
            for (int i = 0; i < n; i++)
            {
                Vector3 pos = new Vector3((float)Math.Sin(i * MathHelper.TwoPi / n + angle), 0.0f, (float)Math.Cos(i * MathHelper.TwoPi / n + angle));

                lights.Add(new Light(pos * 25, colours[randomNumber.Next(0, 9)], 15));

                pos = new Vector3((float)Math.Sin(i * MathHelper.TwoPi / n - angle), 0.0f, (float)Math.Cos(i * MathHelper.TwoPi / n - angle));

                lights.Add(new Light(pos * 55, colours[randomNumber.Next(0, 9)], 20));

                pos = new Vector3((float)Math.Sin(i * MathHelper.TwoPi / n + angle), 0.0f, (float)Math.Cos(i * MathHelper.TwoPi / n + angle));

                lights.Add(new Light(pos * 85, colours[randomNumber.Next(0, 9)], 20));

                pos = new Vector3((float)Math.Sin(i * MathHelper.TwoPi / n - angle), 0.0f, (float)Math.Cos(i * MathHelper.TwoPi / n - angle));

                lights.Add(new Light(pos * 115, colours[randomNumber.Next(0, 9)], 20));

                //randomNumber = new Random();
            }
        }

        public void removeLights()
        {
            if (lights.Count > 0)
            {
                for (int i = 0; i < 8; i++)
                {
                    lights.RemoveAt(lights.Count - 1);
                }
            }
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            float angle = (float)gameTime.TotalGameTime.TotalSeconds;

            int n = 2;

            if (lights.Count > 0)
            {
                for (int i = 0; i < lights.Count / 8; i++)
                {
                    angle *= 1.2f;

                    for (int k = 0; k < 2; k++)
                    {
                        Vector3 posTemp = new Vector3((float)Math.Sin(k * MathHelper.TwoPi / n + angle), 0.0f, (float)Math.Cos(k * MathHelper.TwoPi / n + angle));

                        lights[i * 8 + k * 4].Position = posTemp * 25;

                        posTemp = new Vector3((float)Math.Sin(k * MathHelper.TwoPi / n - angle), 0.0f, (float)Math.Cos(k * MathHelper.TwoPi / n - angle));
                        lights[i * 8 + k * 4 + 1].Position = posTemp * 55;

                        posTemp = new Vector3((float)Math.Sin(k * MathHelper.TwoPi / n + angle), 0.0f, (float)Math.Cos(k * MathHelper.TwoPi / n + angle));
                        lights[i * 8 + k * 4 + 2].Position = posTemp * 85;

                        posTemp = new Vector3((float)Math.Sin(k * MathHelper.TwoPi / n - angle), 0.0f, (float)Math.Cos(k * MathHelper.TwoPi / n - angle));
                        lights[i * 8 + k * 4 + 3].Position = posTemp * 115;
                    }
                }
            }

            base.Update(gameTime);
        }

        public List<Light> LightList
        {
            get { return lights; }
            set { lights = value; }
        }

        /*
        public Vector3[] PositionList
        {
            get { return lights
        }*/

        public Color[] Colours
        {
            get { return colours; }
        }
    }
}